package com.qk.management.web.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * &#064;author：zzq
 * &#064;createTime：2025/10/11 12:14
 * &#064;description： 演示拦截器入门
 * 1. 创建拦截器类,实现HandlerInterceptor接口
 * 2. 配置拦截器
 * 2.1  @Configuration注解,作用为配置类
 * 3. 指定拦截路径
 */
@Slf4j
@Component
public class MyInterceptor implements HandlerInterceptor {
    /*
     * 前置处理器 请求到达控制器之前执行
     * @param request 请求对象
     * @param response 响应对象
     * @param handler 处理器对象 本质上是杨访问的controller方法对象
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("preHandle");
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    /*
     * 后置处理器 请求到达控制器之后执行
     * @param request 请求对象
     * @param response 响应对象
     * @param handler 处理器对象 本质上是访问的controller方法对象
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("postHandle");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    /*
     * 拦截器完成
     * @param request 请求对象
     * @param response 响应对象
     * @param handler 处理器对象 本质上是访问的controller方法对象
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("afterCompletion");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
